//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using WCSFContrib.PageFlow.WorkflowFoundation.Tests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.PageFlow.WorkflowFoundation.Activities;

namespace WCSFContrib.PageFlow.WorkflowFoundation.Tests
{
	[TestClass]
	public class WorkflowFoundationPageFlowDefinitionFixture
	{
		[TestMethod]
		[ExpectedException(typeof (ArgumentNullException))]
		public void PageFlowTypeCannotBeNull()
		{
			new WorkflowFoundationPageFlowDefinition(null);
		}

		[TestMethod]
		public void CanCreateInstance()
		{
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(new MockUnconstrainedPageFlow());

			Assert.IsNotNull(definition);
			Assert.IsTrue(definition is IPageFlowDefinition);
		}

		[TestMethod]
		public void ContainsPage()
		{
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(new MockUnconstrainedPageFlow());
			bool containsPage = definition.ContainsPage(new Page("PageA", "~/pageA.aspx"));

			Assert.IsTrue(containsPage);
		}

		[TestMethod]
		[ExpectedException(typeof (ArgumentNullException))]
		public void PageArgumentContainsPageCannotBeNull()
		{
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(new MockUnconstrainedPageFlow());
			definition.ContainsPage(null);
		}

		[TestMethod]
		public void AbandonableShouldBeEqualsWorkflowFoundationTypeAbandonable()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);
			Assert.AreEqual(pageFlowDefinition.Abandonable, definition.Abandonable);
		}

		[TestMethod]
		public void AbortPageUrlShouldBeEqualsWorkflowFoundationTypeAbortUrl()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);
			Assert.AreEqual(pageFlowDefinition.AbortUrl, definition.AbortPage.Url);
		}

		[TestMethod]
		public void ConstrainedShouldBeEqualsWorkflowFoundationTypeConstranied()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);
			Assert.AreEqual(pageFlowDefinition.Constrained, definition.Constrained);
		}

		[TestMethod]
		public void NameShouldBeEqualsWorkflowFoundationTypeName()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);
			Assert.AreEqual(pageFlowDefinition.Name, definition.Name);
		}

		[TestMethod]
		public void NotRunningUrlShouldBeEqualsWorkflowFoundationTypeNotRunningUrl()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);
			Assert.AreEqual(pageFlowDefinition.NotRunningUrl, definition.NotRunningRedirect);
		}

		[TestMethod]
		public void NumberOfPagesShouldBeEqualsWorkflowFoundationTypePageActivityCount()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.AreEqual(pageFlowDefinition.Activities.Count, definition.Pages.Count);
			Assert.AreEqual(pageFlowDefinition.Activities[0].Name, definition.Pages[0].Name);
			Assert.IsNotNull(pageFlowDefinition.Activities[definition.Pages[1].Name]);
		}

		[TestMethod]
		public void StartPageShouldBeWorkflowFoundationTypeInitialStateName()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.AreEqual(pageFlowDefinition.InitialStateName, definition.StartPage.Name);
		}

		[TestMethod]
		public void PageFlowTypeShouldBeTypeOfWorkflowFoundationDefinition()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.AreEqual(pageFlowDefinition.GetType(), definition.PageFlowType);
		}

		[TestMethod]
		public void ShouldKnowIfContainsAnSpecificUrl()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.IsFalse(definition.ContainsUrl("~/notContained.aspx"));
		}

		[TestMethod]
		public void UrlContainsUrlCouldBeNull()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.IsFalse(definition.ContainsUrl(null));
		}

		[TestMethod]
		public void ShouldReturnAPageFromItsName()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.AreEqual(pageFlowDefinition.InitialStateName, definition.GetPage(pageFlowDefinition.InitialStateName).Name);
		}

		[TestMethod]
		public void ShouldReturnAPageFromItsNameEvenIfItsInDifferentCase()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.AreEqual(pageFlowDefinition.InitialStateName,
			                definition.GetPage(pageFlowDefinition.InitialStateName.ToUpper()).Name);
		}

		[TestMethod]
		public void ShouldGetAPageWithAGivenUrl()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.AreEqual(pageFlowDefinition.InitialStateName, definition.GetPageFromUrl("~/pageA.aspx").Name);
		}


		[TestMethod]
		public void ShouldGetAPageWithAGivenUrlEvenIfItsOnDifferentCase()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Assert.AreEqual(pageFlowDefinition.InitialStateName, definition.GetPageFromUrl("~/PAGEA.ASPX").Name);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void GetPageShouldThrowWhenPassedNullString()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);
			String s = null;
			Page p = definition.GetPage(s);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void GetPageFromUrlShouldThrowWhenPassedNull()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			IPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			Page p = definition.GetPageFromUrl(null);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void GetPageShouldThrowWhenPassedNull()
		{
			MockUnconstrainedPageFlow pageFlowDefinition = new MockUnconstrainedPageFlow();
			WorkflowFoundationPageFlowDefinition definition = new WorkflowFoundationPageFlowDefinition(pageFlowDefinition);

			PageState state = null;
			Page p = definition.GetPage(state);
		}
	}
}
